import React from 'react';
import { inject, observer } from 'mobx-react';
import {
  WeaTop,
  WeaRightMenu,
  WeaErrorPage,
  WeaTools,
  WeaNewScroll,
  WeaLocaleProvider,
} from 'ecCom';
import DocOfAuthorContent from './docofauthor/DocOfAuthorContent';
import Condition from './docofauthor/Condition';
import { toJS } from 'mobx';
import { formatter } from '../util';

const getLabel = WeaLocaleProvider.getLabel;
const store = [
  'routing',
  'docOfAuthorStore',
];
@inject(...store)

@observer
class DocOfAuthor extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      condition: { // 默认搜索条件
        doccreatedateselect: '7',
        doccreatedatefrom: '',
        doccreatedateto: '',
        deptType: '3',
        branch_id: '',
        dept_id: '',
        hrm_id: '',
        statusType: '2',
      },
      collectParams:{
        favname: getLabel(132041, '著者文档数量'),
        favouritetype: 2,
        objid: 0,
        importantlevel: 1,
        link: 'wui/index.html#/main/document/docofauthor',
      },
      dropMenuProps:{
        collectParams: {
          favname: getLabel(132041, '著者文档数量'),
          favouritetype: 2,
          objid: 0,
          importantlevel: 1,
          link: 'wui/index.html#/main/document/docofauthor',
        },
      }
    };
  }
  componentDidMount() {
    const { location, docOfAuthorStore } = this.props;
    const { query = {}, state = {} } = location;
    const { prePath = '', prePathName = '' } = state;
    prePath && prePathName && docOfAuthorStore.setState({ prePath, prePathName });
    const { getCondition } = docOfAuthorStore;
    getCondition({}, query);
  }
  componentWillReceiveProps() {

  }

  componentWillUnmount() {
  }
  onRightMenuClick() {
    const Arr = [];
    return Arr;
  }
  getRightMenu() {
    const Arr = [
     ];
    return Arr;
  }
  getButtons() {
    const arr = [];
    return arr;
  }
  getBreadcrumb() {
    const { docOfAuthorStore, routing, location } = this.props;
    const { prePath, prePathName } = docOfAuthorStore;
    let arr = [];
    prePath && prePathName && arr.push({
      name: prePathName,
      onClick: () => {
        routing.push({ pathname: prePath, state: {} });
        docOfAuthorStore.setState({ prePath: '', prePathName: '' });
      },
    });
    arr.push({
      name: getLabel(16600, '著者文档数量'),
    });
    return arr;
  }
  getDatas = (condition) => {
    // 修改浏览框的replaceData，以保存上一次查询条件
    const { docOfAuthorStore } = this.props;
    const { getReportPeopleData, getCreateDocNum
      , getMostDocCategory, getReportData } = docOfAuthorStore;
    getReportPeopleData(condition);
    getCreateDocNum(condition);
    getMostDocCategory(condition);
    getReportData(condition);
    this.setState({ condition });
  }

  onChange=(params) => {
    let { condition } = this.state;
    const { docOfAuthorStore } = this.props;
    let { subdepartment, department } = docOfAuthorStore;
    
    if (params.deptType && params.deptType === '4' && (!params.hrm_id)) {
    } else if (params.doccreatedateselect && params.doccreatedateselect === '6'
      && ((!params.doccreatedatefrom) && (!params.doccreatedateto))) {
    } else {
      if (params.deptType && params.deptType === '2' && (!params.branch_id)) {
        condition.branch_id = subdepartment;
      }
      if (params.deptType && params.deptType === '3' && (!params.dept_id)) {
        condition.dept_id = department;
      }
      for (let item in params) {
        condition[item] = params[item];
      }
      this.getDatas(condition);
    }
  }

  render() {
    let { condition } = this.state;
    const { docOfAuthorStore, location } = this.props;
    let { reportData, loading, title, conditions, loadingChart
      , loadingRPD, loadingCDM, loadingMDC, subdepartment, department } = docOfAuthorStore;
    const loadingNow = loading || loadingChart || loadingRPD || loadingCDM || loadingMDC;
    conditions = toJS(conditions);
    // 图表_初始化数据
    let ydata = [];
    let xdata = [];
    reportData.forEach((item) => {
      ydata.push(item.docNum);
      xdata.push(`${formatter(item.doccreatername)}(${getLabel(30042, '人员')}ID : ${item.doccreaterid})`);
    });
    return (
      <div className="wea-doc-count">
        <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@s1xexj`}
          collectParams={this.state.collectParams}
          datas={this.getRightMenu()}
          onClick={this.onRightMenuClick}
        >
          <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@r7ajtt`}
            breadcrumb={this.getBreadcrumb()}
            title={title}
            loading={loadingNow}
            icon={<i className="icon-coms-doc" />}
            iconBgcolor="#df583a"
            buttons={this.getButtons()}
            buttonSpace={10}
            showDropIcon={true}
            dropMenuDatas={this.getRightMenu()}
            dropMenuProps={this.state.dropMenuProps}
            onDropMenuClick={this.onRightMenuClick}
          >
            <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@cnxuoq`} height={'100%'}>
              <Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@q2z1ci`} config={conditions}
                onChange={
                this.onChange
              }
                // beforeInit={
                //   (config, lastValueMap, cb) => {
                //     cb(lastValueMap);
                //   }
                // }

              />
              <DocOfAuthorContent ecId={`${this && this.props && this.props.ecId || ''}_DocOfAuthorContent@7rfndk`} ydata={ydata} xdata={xdata} query={location.query} />
            </WeaNewScroll>
          </WeaTop>
        </WeaRightMenu>
      </div>
    );
  }
}

export default WeaTools.tryCatch(React,
  props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@5kfygc`} msg={props.error ? props.error : getLabel(385218, '对不起，该页面异常，请联系管理员！')} />,
  { error: '' },
)(DocOfAuthor);

